<% empty_permission = com.thoughtworks.go.config.Authorization::PresentationElement.new("", scope[:permission_type]) %>

<textarea id="<%= scope[:permission_type] %>_users_and_roles_template" class="template">
  <%= scope[:f].fields_for com.thoughtworks.go.config.PipelineConfigs::AUTHORIZATION, empty_permission, :index => "" do |permission_form| %>
        <td>
            <%= permission_form.text_field com.thoughtworks.go.config.Authorization::PresentationElement::NAME, :class => "form_input permissions_#{scope[:permission_type]}_name", :id => nil %>
          <%= permission_form.hidden_field com.thoughtworks.go.config.Authorization::PresentationElement::TYPE, :id => nil %>
        </td>
      <%= permission_form.fields_for com.thoughtworks.go.config.Authorization::PRIVILEGES, empty_permission, :index => "" do |privileges_form| %>
            <td>
                <%= check_box_tag com.thoughtworks.go.config.Authorization::PresentationElement::VIEW_PRIVILEGE, 1, true,
                                  :id => "viewPrivilege_#{scope[:permission_type]}_name", :title => 'View' %>
              <%= privileges_form.hidden_field com.thoughtworks.go.config.Authorization::PresentationElement::VIEW_PRIVILEGE, :value => com.thoughtworks.go.config.Authorization::PrivilegeState::ON, :id => nil %>
            </td>
            <td>
                <%= check_box_tag com.thoughtworks.go.config.Authorization::PresentationElement::ADMIN_PRIVILEGE, 0, false,
                                  :id => "adminPrivilege_#{scope[:permission_type]}_name", :title => 'Admin' %>
              <%= privileges_form.hidden_field com.thoughtworks.go.config.Authorization::PresentationElement::ADMIN_PRIVILEGE, :id => nil %>
            </td>
            <td>
				<%= action_icon({:type => "remove", :class => "delete_parent"}) %>
            </td>
        <% end %>
    <% end %>
</textarea>
<table class="group_permissions_table users_and_roles list_table">

  <thead>
  <tr>
    <th class="name">Name</th>
    <th>View</th>
    <th>Admin</th>
    <th class="remove">Remove</th>
  </tr>
  </thead>
  <tbody class="<%= scope[:permission_type] %>_users_and_roles">

  <% scope[:permissions].each do |permission| %>
      <%= scope[:f].fields_for com.thoughtworks.go.config.PipelineConfigs::AUTHORIZATION, permission, :index => "" do |permission_form| %>
          <tr id="<%= "#{scope[:permission_type]}_#{permission.getName()}" -%>">
            <td>
              <%= permission_form.text_field com.thoughtworks.go.config.Authorization::PresentationElement::NAME,  :class => "form_input permissions_#{scope[:permission_type]}_name", :id => nil %>
              <%= permission_form.hidden_field com.thoughtworks.go.config.Authorization::PresentationElement::TYPE, :id => nil %>
              <%= error_message_on(permission, com.thoughtworks.go.config.Authorization::PresentationElement::NAME, :css_class => "name_value_error") %>
            </td>
            <%= permission_form.fields_for com.thoughtworks.go.config.Authorization::PRIVILEGES, permission, :index => "" do |privileges_form| %>
                <td>
                  <%= check_box_tag com.thoughtworks.go.config.Authorization::PresentationElement::VIEW_PRIVILEGE, 0, false,
                                    :id => "viewPrivilege_#{scope[:permission_type]}_#{permission.getName()}", :title => 'View' %>
                  <%= privileges_form.hidden_field com.thoughtworks.go.config.Authorization::PresentationElement::VIEW_PRIVILEGE, :id => nil %>
                </td>
                <td>
                  <%= check_box_tag com.thoughtworks.go.config.Authorization::PresentationElement::ADMIN_PRIVILEGE, 0, false,
                                    :id => "adminPrivilege_#{scope[:permission_type]}_#{permission.getName()}", :title => 'Admin' %>
                  <%= privileges_form.hidden_field com.thoughtworks.go.config.Authorization::PresentationElement::ADMIN_PRIVILEGE, :id => nil %>
                </td>
                <td>
                  <%= action_icon({:type => "remove", :class => "delete_parent"}) %>
                </td>
            <% end %>
          </tr>

      <% end
         end %>
  </tbody>
</table>
<%= action_icon({:type => "add", :href => "javascript:void(0);", :id => "#{scope[:permission_type]}_add_users_and_roles", :class => "skip_dirty_stop", :text => "Add #{scope[:permission_type].to_s.downcase} permission" }) %>
<script type="text/javascript">
    Util.on_load(function () {
        function hookupPermissionsAutoComplete() {
            jQuery(<%=="\".permissions_#{scope[:permission_type]}_name\""%>).autocomplete(<%== scope[:autocomplete_suggestions] %>, {
                minChars: 0,
                width: 248,
                matchContains: "word",
                autoFill: false});
        }

      function setupVarForm() {
        var rowCreator = new EnvironmentVariables.RowCreator(jQuery('#<%= scope[:permission_type] %>_users_and_roles_template'), 'tr', '.delete_parent');
        var variables = new EnvironmentVariables(
          jQuery('tbody.<%= scope[:permission_type] %>_users_and_roles'),
          rowCreator,
          null,
          function(inputs) {
            inputs.dirty_form();
            var groupPermissions = new GroupPermissions("<%=com.thoughtworks.go.config.Authorization::PrivilegeState::ON-%>", "<%=com.thoughtworks.go.config.Authorization::PrivilegeState::OFF-%>",
              "<%=com.thoughtworks.go.config.Authorization::PrivilegeState::DISABLED-%>");
            groupPermissions.bindHandlers();
            hookupPermissionsAutoComplete();
          },
          function(row) {
            row.parents("form.dirtyform").data("dirty", true);
          });

        variables.registerAddButton(jQuery("#<%= scope[:permission_type] %>_add_users_and_roles"));
        variables.addDefaultRow();
      }

        setupVarForm();
        hookupPermissionsAutoComplete();
    });
</script>
